package com.wgworkshop.pems.service;

import com.wgworkshop.pems.entity.StudentEntity;
import com.wgworkshop.pems.model.*;
import com.wgworkshop.pems.repository.AttendanceRepository;
import com.wgworkshop.pems.repository.ClassArrangeRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
 * 统计信息服务实现类。
 * Created by wlc on 2017/12/26.
 */
@Service
public class StatisticsServiceImpl implements StatisticsService {
    private final ClassArrangeRepository classArrangeRepository;
    private final AttendanceRepository attendanceRepository;

    @Autowired
    public StatisticsServiceImpl(ClassArrangeRepository classArrangeRepository,
                                 AttendanceRepository attendanceRepository) {
        this.classArrangeRepository = classArrangeRepository;
        this.attendanceRepository = attendanceRepository;
    }

    @Override
    public List<AttendanceCountByClassView> getAttendanceCount(Date start, Date end) {
        Iterable<ClassArrangeByClassRecord> classArranges = classArrangeRepository.getArrangeByClass();

        List<AttendanceCountByClassView> result = new ArrayList<>();
        classArranges.forEach((classArrange) -> {
            AttendanceCountByClassView view = new AttendanceCountByClassView();
            view.setClassInfo(classArrange);

            List<AttendanceCountByClassRecord> attendanceList = new ArrayList<>();
            Iterable<AttendanceCountByClassRecord> records = attendanceRepository.getCountByClass(start, end, classArrange.getWeek(), classArrange.getClassNum());
            records.forEach(attendanceList::add);

            view.setAttendanceCountList(attendanceList);

            result.add(view);
        });

        return result;
    }

    @Override
    public List<AttendanceDetailByClassView> getAttendanceDetail(Date start, Date end, int week, int classNum) {
        Iterable<Object[]> records = attendanceRepository.getDetailByClass(start, end, week, classNum);

        Map<StudentEntity, List<AttendanceDetailByClassRecord>> map = StreamSupport.stream(records.spliterator(), false)
                .map((record) -> {
                    StudentEntity studentEntity = (StudentEntity) record[0];
                    Date date = (Date) record[1];
                    return new AttendanceDetailByClassRecord(studentEntity, date);
                })
                .collect(Collectors.groupingBy(AttendanceDetailByClassRecord::getStudent));

        List<AttendanceDetailByClassView> result = new ArrayList<>();
        map.forEach((key, value) -> {
            AttendanceDetailByClassView view = new AttendanceDetailByClassView();

            StudentModel studentModel = new StudentModel();
            BeanUtils.copyProperties(key, studentModel);
            view.setStudentInfo(studentModel);

            List<Date> courseDateList = value.stream().map(AttendanceDetailByClassRecord::getCourseDate)
                    .collect(Collectors.toList());

            view.setAttendanceDetailList(courseDateList);
            result.add(view);
        });

        return result;
    }
}
